package top.fzqblog.firstapp;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import top.fzqblog.util.FileUtils;


/**
 * Created by 抽离 on 2017/10/8 0008.
 */

public class VideoTest extends AppCompatActivity {

    @BindView(R.id.videoView)
    public VideoView videoView;

    @OnClick({R.id.play, R.id.rePlay, R.id.pause, R.id.getVideo, R.id.netVideo})
    public void videoControl(Button button){
        switch (button.getId()){
            case R.id.play:
                if(!videoView.isPlaying()){
                    videoView.start();
                }
                break;
            case R.id.rePlay:
                if(videoView.isPlaying()){
                    videoView.resume();//重新播放
                }
                break;
            case R.id.pause:
                if(videoView.isPlaying()){
                    videoView.pause();
                }
                break;
            case R.id.getVideo:
                if(!videoView.isPlaying()){
                    getVideo();
                }
            case R.id.netVideo:
                if(!videoView.isPlaying()){
                    setSource("http://45.43.9.11:8215/media/videos/mobile/16626.mp4?st=jpW56nUTx8SJs_eQA4SwMQ&e=1507410442");
                }
                break;
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_layout);
        ButterKnife.bind(this);
    }

    private void getVideo(){
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
        else{
            initMedia();
        }
    }

    private void initMedia() {
        Observable.OnSubscribe<File> observable = new Observable.OnSubscribe<File>() {
            @Override
            public void call(Subscriber<? super File> subscriber) {
                try {
                    InputStream in = getAssets().open("1.mp4");
                    File file = FileUtils.copyToDownloadFile(in, "1.mp4");
                    subscriber.onNext(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        Observable
                .create(observable)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Action1<File>() {
                    @Override
                    public void call(File file) {
                        Toast.makeText(VideoTest.this, "获取视频" + file.getPath(), Toast.LENGTH_LONG).show();
                    }
                })
                .subscribe(file -> {
                        setSource(file.getPath());
                });
    }

    private void setSource(String path){
        videoView.setVideoPath(path);
        Toast.makeText(this, "视频时长：" + (videoView.getDuration()/(1000 )) + "秒", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 1:
                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                    initMedia();
                }
                else{
                    Toast.makeText(this, "没有读写权限无法播放！", Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
        }
    }
}
